/* comunication with Megatune and teminalv */


/* Read error code of the uart HW */
char   uart_err   (void)
{
   char err;
   err=RCSTA & 0b00000110;
   return err;
}

#INT_RDA
void rx_int (void)
   {
         static   unsigned   int8 counter;
         unsigned   int8   rs_tmp,i;
         i=uart_err();
         switch (i)
            {
               case 2:
                  dbg_puts("uart_err2");
                  bit_clear(RCSTA,4);
                  bit_set(RCSTA,4);
               break;
               
               case 4:
                  dbg_puts("uart_err4");
                  return;
               break;
            }
            
            rs_tmp=getc();
            //putc(rs_tmp);  //terminal echo
           switch (txmode)
            {  
           case 0:
            switch (rs_tmp)
               {
                  case '?':
                  case 0x15:   // bootloader start code
                    RST=1;   // set reset bit
                  break;
                  case 13:   // return code
                    cmd_rcv=1;
#if (3 != __BUILD_TYPE__)
                    rs_string[counter]=0;
#endif
                    counter=0;                  
                  break;
                  case 'c':
                     counter=0;
                    txcnt = 0;         
                    txmode = 'c';
                    txgoal = 2;        // seconds is 1st 2 bytes of outpc structure
#if (3 != __BUILD_TYPE__)
                    TXREG = *(char *)&out_parm;
#else
                    TXREG = *(char *)&in.in;//out_parm;
#endif
                    enable_interrupts(INT_TBE);   // xmit enable & xmit interrupt enable
                  break;
                  case 'a':
                  case 'A':
                    counter=0;
                    txcnt = 0;         
                    txmode = 'c';
#if (3 != __BUILD_TYPE__)
                    txgoal = sizeof(out_parm);       
                    TXREG = *(char *)&out_parm;
#else
                    txgoal = sizeof(in.in);       
                    TXREG = *(char *)&in.in;//out_parm;
#endif
                    enable_interrupts(INT_TBE);   // xmit enable & xmit interrupt enable
                  break;
                  case 'Q':
                    counter=0;
                    txgoal = sizeof(RevNum);// 20      
                    txmode = 'Q';       
                    TXREG = RevNum[0];
                    enable_interrupts(INT_TBE);   // xmit enable & xmit interrupt enable
                  break;
                  case 'S':
                    counter=0;
                    txgoal = sizeof(Signature);// 32      
                    txmode = 'S';       
                    TXREG = Signature[0];
                    enable_interrupts(INT_TBE);   // xmit enable & xmit interrupt enable
                  break;
                  case 'v':         // send back all user inputs as they exist in RAM
                  case 'V':
                    counter=0;
                    txcnt = 0;
                    txmode = 3;
                    txgoal = sizeof(in_parm);
                    TXREG = *(char *)&in_parm;
                    counter = 0;
                    enable_interrupts(INT_TBE);  // xmit enable & xmit interrupt enable
               break;
                case 'w':        // receive new user input and store in offset location
                    counter=0;
                    txmode = 5;
                    rd_wr = 1;
                    dbg_puts("ok begin");        
                break;
                case 'b':        // burn setings stored in RAM into FLASH memory
                    counter=0;
                    burn_flag = 1; 
                break;
                default:
                     if (rs_tmp=='t'||rs_tmp=='d'||rs_tmp=='z')
                        {
                           counter=0;
#if (3 != __BUILD_TYPE__)
                           rs_string[counter]=rs_tmp;
#endif
                        }
                     else if (counter < STRING_LENGTH)
                        {
#if (3 != __BUILD_TYPE__)
                           rs_string[counter]=rs_tmp;
#endif
                           counter++;
                        }
                     else
                        {
#if (3 != __BUILD_TYPE__)
                           rs_string[counter]=0;   // string null termination
#endif
                           dbg_puts("ErR");
                           counter=0;
                           txmode = 0;
                        }             
                  break;
               }
            break;
        case 5:
           //puts("ok1");
         counter=0;
           //rxoffset = (rs_tmp << 8); // byte offset(msb) from start of inpram
           rxoffset = (int16)rs_tmp;
           txmode++;
        break;

      case 6:
         //puts("ok2");
         counter=0;
           //rxoffset |= rs_tmp;    // byte offset(lsb) from start of inpram
           rxoffset |= ((int16)rs_tmp << 8);
            txmode++;
        break;
     
      case 7:
         //puts("ok3");
         counter=0;
        //   rxnbytes = (rs_tmp << 8);    // no. bytes (msb)
           rxnbytes = (int16)rs_tmp;
           txmode++;
        break;

      case 8:
         //puts("ok4");
         counter=0;
        //   rxnbytes |= rs_tmp;      // no. bytes (lsb)
           if(rs_tmp==1&&rxnbytes==0) //ms-pach to recive 1 byte data
              {
                 rxnbytes = (int16)rs_tmp;
              }
           else { rxnbytes |= ((int16)rs_tmp << 8); }
           txmode++;
           rxcnt = 0;
       break;

      case 9:
           counter=0;
           
           *((char *)&in_parm + (rxoffset + rxcnt)) = rs_tmp;
           rxcnt++;
           if(rxcnt >= rxnbytes)
              {
                 txmode = 0;         // done receiving all the data
           //      puts("ok");
            /*      else  
                     {
                       txcnt = 0;            // send back inpram data just written
                       txmode = 3;            //    for verification
                       txgoal = rxnbytes;
                       TXREG = *((char *)&in_parm  + rxoffset);   
                       enable_interrupts(INT_TBE);  // xmit enable & xmit interrupt enable
                     }*/
              }
        break;
            }
            return;
    }
#INT_TBE
void tx_int (void)
{
    txcnt++;
    if (txcnt < txgoal) {   
        switch (txmode) {
            case 3:
                TXREG = *((char *)&in_parm + txcnt);         
                break;
            case 'S':
                TXREG = Signature[txcnt];         
                break;
            case 'Q':
                TXREG = RevNum[txcnt];         
                break;
            default:
#if (3 != __BUILD_TYPE__)
                TXREG = *((char *)&out_parm + txcnt);
#else
                TXREG = *((char *)&in.in + txcnt);
#endif
                break;
        }
    } else {    // done transmitting
        txcnt = txgoal = txmode = 0;
        disable_interrupts(INT_TBE);  // xmit disable & xmit interrupt disable
    }
    return;
}

